<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<div class="row stu-info-title">
	<div class="col-md-1"></div>
	<div class="col-md-10">
		<div id="small-class-name"></div>
		<input type="hidden" name="classId" />
	</div>
	<div class="col-md-1"></div>
</div>
<div class="row">
	<div class="col-md-1"></div>
	<div class="col-md-10" style="text-align:right;">
		<shiro:hasRole name="teacher">
			<button type="button" class="btn btn-primary" id="addstu">添加学生</button>
		</shiro:hasRole>
	</div>
	<div class="col-md-1"></div>
</div>
<div class="row">
	<!--已存在学生列表-->
	<div class="col-md-1"></div>
	<div class="col-md-10">
		<table class="table table-hover">
			<thead>
				<tr>
					<th>序号</th>
					<th>学号</th>
					<th>姓名</th>
					<shiro:hasRole name="teacher">
						<th><input type="checkbox" name="" id="checkHasSmallAll" value="" />全选</th>
					</shiro:hasRole>
				</tr>
			</thead>
			<shiro:hasRole name="teacher">
				<tbody id="smallStudentListTeacher">
				</tbody>
			</shiro:hasRole>
			<shiro:hasRole name="assistant">
				<tbody id="smallStudentListAssistant">
				</tbody>
			</shiro:hasRole>
			
		</table>
	</div>
	<div class="col-md-1"></div>
</div>
<div class="row">
	<div class="col-md-1"></div>
	<div class="col-md-5" style="text-align:left;">
		<shiro:hasRole name="teacher">
			<button class="btn btn-primary" id="smallStudentDelete">删除</button>
		</shiro:hasRole>
	</div>
	<div class="col-md-5">
		<div class="page-content" id="smallStudentPage">
			<nav class="pagination" role="navigation" style="text-align:right;">
				<a class="last-page" id="last-page-has-small">
					<i class="fa fa-angle-left"></i>
				</a>
				<span class="page-number" id="page-number-has-small">第1页/共7页</span>
				<a class="next-page" id="next-page-has-small">
					<i class="fa fa-angle-right"></i>
				</a>
			</nav>
		</div>
	</div>
	<div class="col-md-1"></div>
</div>

<script>
$(function() {
	/*点击--“小班添加学生”按钮--*//*小班添加学生弹出框*/
	$("#addstu").click(function(){
		layer.open({
			type: 1,
			title: '添加学生',
			fixed: false, //不固定
			maxmin: true,
			closeBtn: 1,
			area: ['80rem', '40rem'],
			content: $("#addstudent"),
			end: function() {
				setTimeout(function(){
					layer.closeAll();
				}, 1000);
            }
		});
		getCSmallStudents(1);
	});
	
	$("#checkHasSmallAll").click(function(){
		//全选状态
		if($("#checkHasSmallAll").is(':checked')){
			$('input[name="deleteSmallStudentId"]').each(function(){
				$(this).prop("checked", true);
			});
		}else{//全不选状态
			$('input[name="deleteSmallStudentId"]').each(function(){
				$(this).prop("checked", false);
			});
		}
	});
	$("#smallStudentDelete").click(smallStudentDelete);
});

var initSmallClassPages = function(pageNo,allpages){
	
	var lastPageObj=$("#last-page-has-small");
	var nextPageObj=$("#next-page-has-small");
	var pageNumberObj=$("#page-number-has-small");
	var lastPage=pageNo-1;
	if(lastPage<=0){//设置disableed
		lastPage=1;
	}else{
		$("#last-page-has-small").bind("click",{pageNo:lastPage},getSmallPage);
	};
	var nextPage = pageNo+1;
	if(nextPage>allpages){
		nextPage=allpages;
	}else{
		$("#next-page-has-small").bind("click",{pageNo:nextPage}, getSmallPage);
	}
	if(allpages <=1){
		$("#smallStudentPage").hide();
	}else {
		$("#smallStudentPage").show();
		pageNumberObj.text("第"+pageNo+"页/共"+allpages+"页");
	}	
}
var getSmallPage = function(event){
	var pageNo=event.data.pageNo; 
	var id=$("input[name='classId']").val();
	getSmallStudentListPage(id,pageNo);
}
var getSmallStudentList = function(id,pageNo){
	$(".student-smallclass").show();
	$(".student-bigclass").hide();
	$(".lab-and-theory").hide();
	getSmallStudentListPage(id,pageNo);
}
var getSmallStudentListPage = function(id,pageNo){
	
	var json={
			classId:id,
			pageNo:pageNo
	};
	$.get(CONTEXT_PATH+"cscontrol/ajaxListView",{
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			$("#small-class-name").text(data.data.classView.className+"学生列表");
			var smallStudentListTeacher = $("#smallStudentListTeacher");
			var smallStudentListAssistant = $("#smallStudentListAssistant");
			$("input[name='classId']").val(data.data.classView.id);
			smallStudentListTeacher.empty();
			smallStudentListAssistant.empty();
    		var s="";
    		var s1="";
    		var students=data.data.students;
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s+="<td><input type=\"checkbox\" name=\"deleteSmallStudentId\" value=\""+item.id+"\" /></td>";
    			s+="</tr>"
    		};
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s1+="<tr>";
    			s1+="<th scope=\"row\">"+(i+1)+"</th>";
    			s1+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s1+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s1+="</tr>"
    		};
    		smallStudentListTeacher.append(s);
    		smallStudentListAssistant.append(s1);
    		initSmallClassPages(data.data.pageNo,data.data.allpages);
		}else{
			alert(data.message);
		}
	}); 
}
var smallStudentDelete = function(){
	var classId=$("input[name='classId']").val();
	var studentIds =[];
	$('input[name="deleteSmallStudentId"]:checked').each(function(){ 
		studentIds.push($(this).val()); 
	});
	if(studentIds.length<=0){
		alert("请选择待删除学生");
		return ;
	}
	var json={
			ids:studentIds,
			keyId:classId
	};
	$.post(CONTEXT_PATH+"cscontrol/batchDelete", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			getSmallStudentList(classId);
		}else{
			alert(data.message);
		}
	});
}
</script>